package com.studentexam.student.service.impl;

import com.studentexam.student.dao.AnswerMapper;
import com.studentexam.student.dao.ExamAnswerMapper;
import com.studentexam.student.model.Answer;
import com.studentexam.student.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private ExamAnswerMapper examAnswerMapper;


    @Override
    public int addAnswer(Answer answer) {
        int returnInt=answerMapper.insertAnswer(answer);

        return returnInt;
    }

    @Override
    public int updateAnswer(Answer answer) {
        int isUpdate=0;
        try{
            isUpdate=answerMapper.updateAnswer(answer);
        }catch (Exception e){
            isUpdate=-1;
            System.out.println("更新答题信息时执行sql报错原因为:"+e.getMessage());
        }
        return isUpdate;
    }

    @Override
    public int delAnswer(int answerid) {
        int returnDel=0;
        try {
            if (examAnswerMapper.getAnswerCount(answerid)< 1) {
                returnDel = answerMapper.delAnswer(answerid);
            }
        }catch (Exception e){
            returnDel=-1;
            System.out.println("删除失败原因为："+e.getMessage());
        }
        return returnDel;
    }

    @Override
    public List<Answer> getByAnswerid(int answerid) {
        List<Answer> answerList=answerMapper.getByAnswerid(answerid);
        return answerList;
    }

    @Override
    public List<Answer> getAllAnswer(Answer answer){
        List<Answer> answerList = answerMapper.getAllAnswer(answer);
        return answerList;
    }
}
